package com.example.customer01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.customer01.mapper.StudentCourseMapper;
import com.example.customer01.pojo.Course;
import com.example.customer01.pojo.StudentCourse;
import com.example.customer01.service.CourseService;
import com.example.customer01.service.StudentCourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 學生選課服務實現類
 */
@Service
public class StudentCourseServiceImpl extends ServiceImpl<StudentCourseMapper, StudentCourse> implements StudentCourseService {

    @Resource
    private StudentCourseMapper studentCourseMapper;

    @Resource
    private CourseService courseService;

    @Override
    public IPage<StudentCourse> getStudentCoursePage(Page<StudentCourse> page, String keyword) {
        LambdaQueryWrapper<StudentCourse> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(keyword)) {
            wrapper.like(StudentCourse::getStatus, keyword)
                    .or()
                    .like(StudentCourse::getSemester, keyword);
        }
        return page(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StudentCourse createStudentCourse(StudentCourse studentCourse) {
        // 檢查課程是否存在
        Course course = courseService.getById(studentCourse.getCourseId());
        if (course == null) {
            throw new RuntimeException("課程不存在");
        }

        // 檢查課程容量
        int enrolledCount = baseMapper.countEnrolledStudents(studentCourse.getCourseId());
        if (enrolledCount >= course.getMaxStudents()) {
            throw new RuntimeException("課程已滿");
        }

        // 檢查是否已選修
        if (checkStudentCourse(studentCourse.getStudentId(), studentCourse.getCourseId())) {
            throw new RuntimeException("已選修該課程");
        }

        // 設置選課狀態
        studentCourse.setStatus("ENROLLED");
        save(studentCourse);
        return studentCourse;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<StudentCourse> batchCreateStudentCourses(List<StudentCourse> studentCourses) {
        for (StudentCourse sc : studentCourses) {
            // 檢查課程是否存在
            Course course = courseService.getById(sc.getCourseId());
            if (course == null) {
                throw new RuntimeException("課程不存在");
            }

            // 檢查課程容量
            int enrolledCount = baseMapper.countEnrolledStudents(sc.getCourseId());
            if (enrolledCount >= course.getMaxStudents()) {
                throw new RuntimeException("課程已滿");
            }

            // 檢查是否已選修
            if (checkStudentCourse(sc.getStudentId(), sc.getCourseId())) {
                throw new RuntimeException("已選修該課程");
            }

            // 設置選課狀態
            sc.setStatus("ENROLLED");
        }

        // 批量保存選課記錄
        saveBatch(studentCourses);
        return studentCourses;
    }

    @Override
    public boolean deleteStudentCourse(Integer id) {
        return removeById(id);
    }

    @Override
    public List<StudentCourse> getStudentCourses(Integer studentId) {
        LambdaQueryWrapper<StudentCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentCourse::getStudentId, studentId);
        return list(wrapper);
    }

    @Override
    public List<StudentCourse> getCourseStudents(Integer courseId) {
        LambdaQueryWrapper<StudentCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentCourse::getCourseId, courseId);
        return list(wrapper);
    }

    @Override
    public boolean checkStudentCourse(Integer studentId, Integer courseId) {
        LambdaQueryWrapper<StudentCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentCourse::getStudentId, studentId)
                .eq(StudentCourse::getCourseId, courseId);
        return count(wrapper) > 0;
    }

    @Override
    public List<StudentCourse> getStudentCoursesWithDetails(Integer studentId) {
        return studentCourseMapper.findStudentCoursesWithDetails(studentId);
    }
} 